package com.LeeCode;

/**
 * 寻找旋转排序数组中的最小值
 */

public class Code153 {
    public static void main(String[] args) {
        int[] nums = {11, 13, 15, 17};
        System.out.println(new Code153().findMin2(nums));
    }

    public int findMin(int[] nums) {
        int n = nums.length;
        int res = nums[0];
        for (int i = 1; i < n; i++) {
            if (nums[i] < res) {
                res = nums[i];
            }
        }
        return res;
    }

    public int findMin1(int[] nums) {
        int left = 0, right = nums.length - 1;
        int mid = 0;
        while (left < right) {
            mid = left + (right - left) / 2;
            if (nums[nums.length - 1] > nums[mid]) {
                right = mid;
            } else if (nums[nums.length - 1] < nums[mid]) {
                left = mid + 1;
            }
        }
        return nums[left];
    }

    public int findMin2(int[] nums) {
        int left = -1, right = nums.length;
        int n = nums.length - 1;

        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[n])
                left = mid;
            else
                right = mid;
        }

        return nums[right];
    }
}
